using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Markup;
using Common;
using Common.Enum;
using SProject;

namespace ProjectView.Windows
{
	public partial class ProjectExportWindow : Window, IComponentConnector
	{
		public ObservableCollection<Project> ProjectList { get; private set; }

		public ProjectExportWindow()
		{
			base.Initialized += ProjectExportWindow_Initialized;
			InitializeComponent();
		}

		private void ProjectExportWindow_Initialized(object sender, EventArgs e)
		{
			ProjectList = new ObservableCollection<Project>();
			comboAllProjectType.ItemsSource = EnumManagement.GetAllDisplayProjectType();
			comboAllProjectType.SelectionChanged += comboAllProjectType_SelectionChanged;
			comboAllProjectType.SelectedIndex = 0;
			listBoxProject.ItemsSource = ProjectList;
			textBoxPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
		}

		private void comboAllProjectType_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			RefreshProjectList();
		}

		private void RefreshProjectList()
		{
			ObservableCollection<Project> observableCollection = null;
			if (comboAllProjectType.SelectedItem != null)
			{
				KeyValuePair keyValuePair = comboAllProjectType.SelectedItem as KeyValuePair;
				if (keyValuePair != null)
				{
					observableCollection = (((EProjectType)keyValuePair.Key != EProjectType.All) ? ProjectManage.GetInstance().GetProjectByType((EProjectType)keyValuePair.Key) : ProjectManage.GetInstance().AllProjectList);
				}
			}
			if (observableCollection == null)
			{
				observableCollection = ProjectManage.GetInstance().AllProjectList;
			}
			if (observableCollection != null)
			{
				ProjectList.Clear();
				for (int i = 0; i < observableCollection.Count; i++)
				{
					ProjectList.Add(observableCollection[i]);
				}
			}
		}

		private void btnEditPath_DefaultButtonClick(object sender, RoutedEventArgs e)
		{
			FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
			folderBrowserDialog.Description = Utility.GetMessage("Project_Memu_File_Export");
			folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
			folderBrowserDialog.ShowNewFolderButton = true;
			if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				textBoxPath.Text = folderBrowserDialog.SelectedPath;
			}
		}

		private void btnAllExport_Click(object sender, RoutedEventArgs e)
		{
			if (!Directory.Exists(textBoxPath.Text))
			{
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_directory_invalid"));
				textBoxPath.Focus();
				textBoxPath.SelectAll();
				return;
			}
			if (ProjectList.Count == 0)
			{
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_project_not_select"));
				listBoxProject.Focus();
				return;
			}
			bool flag = false;
			foreach (Project project in ProjectList)
			{
				if (!isPrj_FileExist(project.BasicInfo.Name) || System.Windows.MessageBox.Show(string.Format(Utility.GetMessage("msg_import_project_exist"), project.BasicInfo.Name), Utility.GetMessage("Project_Memu_File_Import"), MessageBoxButton.YesNo) != MessageBoxResult.No)
				{
					project.SaveAs(textBoxPath.Text);
					flag = true;
				}
			}
			if (flag)
			{
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_project_export_sussess"));
			}
			Close();
		}

		private void btnExport_Click(object sender, RoutedEventArgs e)
		{
			if (!Directory.Exists(textBoxPath.Text))
			{
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_directory_invalid"));
				textBoxPath.Focus();
				textBoxPath.SelectAll();
				return;
			}
			if (listBoxProject.SelectedItems.Count == 0)
			{
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_project_not_select"));
				listBoxProject.Focus();
				return;
			}
			bool flag = false;
			foreach (Project selectedItem in listBoxProject.SelectedItems)
			{
				if (!isPrj_FileExist(selectedItem.BasicInfo.Name) || System.Windows.MessageBox.Show(string.Format(Utility.GetMessage("msg_import_project_exist"), selectedItem.BasicInfo.Name), Utility.GetMessage("Project_Memu_File_Import"), MessageBoxButton.YesNo) != MessageBoxResult.No)
				{
					flag = true;
					selectedItem.SaveAs(textBoxPath.Text);
				}
			}
			if (flag)
			{
				System.Windows.MessageBox.Show(Utility.GetMessage("msg_project_export_sussess"));
			}
			Close();
		}

		private bool isPrj_FileExist(string Name)
		{
			return (from s in new DirectoryInfo(textBoxPath.Text).GetFiles("*" + Project.ProjectFileDotExt)
				where s.Name == Name + Project.ProjectFileDotExt
				select s).Count() > 0;
		}

		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}
	}
}
